from flask import Flask
from flask_admin.contrib.sqla import ModelView
from flask_admin.menu import MenuLink

import settings
from apps.admins.admin_views import admin_tp, MyAdminIndexView

from apps.apis.dui_huan.cha_xun import chaxun_bp
from apps.apis.dui_huan.chong_zhi import chongzhi_bp
from apps.apis.dui_huan.dui_huan import duihuan_bp
from apps.apis.dui_huan.test import test_bp
from apps.apis.dui_huan.ti_xian import tixian_bp
from apps.apis.dui_huan.zhi_fu import zhifu_bp
from apps.apis.sou_suo.hunt_api import hunt_bp
from apps.apis.yong_hu.user_api import user_bp
from apps.models.admin_model import Users
from apps.models.heyue_model import Agreement
from apps.models.moey_model import WalletMoney
from apps.models.touzhu_model import Pour
from apps.models.user_model import User
from exts import db, redis_cache, admin, login_manager, babel, search


def create_app():
    app = Flask(__name__,template_folder='../templates')# 注册模板显示index.html页面，template_folder='../templates'
    app.config.from_object(settings)

    # 数据库的初始化
    babel.init_app(app)
    db.init_app(app)
    search.init_app(app)
    admin.init_app(app, index_view=MyAdminIndexView())


    login_manager.init_app(app)
    admin.add_view(ModelView(User, db.session,name='用户'))
    admin.add_view(ModelView(Agreement, db.session,name='合约'))
    admin.add_view(ModelView(WalletMoney, db.session,name='钱包'))
    admin.add_view(ModelView(Pour, db.session,name='下注'))
    redis_cache.init_app(app, config=settings.REDIS_CONFIG)
    admin.add_sub_category(name='链接',parent_name="Other")
    admin.add_link(MenuLink(name='回家', url='/', category='链接'))
    admin.add_link(MenuLink(name='谷歌', url='http://www.google.com/', category='链接'))

    # 蓝图注册
    app.register_blueprint(test_bp)

    app.register_blueprint(admin_tp)
    # app.register_blueprint(admin)
    # app.register_blueprint(admin)
    app.register_blueprint(hunt_bp)
    app.register_blueprint(user_bp)

    app.register_blueprint(duihuan_bp)
    app.register_blueprint(chongzhi_bp)
    app.register_blueprint(tixian_bp)
    app.register_blueprint(chaxun_bp)
    app.register_blueprint(zhifu_bp)

    return app


